$(function(){
	var catList = $('#category-list'),
		catDetails = $('#category-details'),
		catEditForm = $('#category-edit-form'),
		EditorMenu = {
			'放弃': function(){
				location.href = 'categories.html';
			},
			'提交': function(){
				$('#category-edit-form').find('button').click();
			}
		},
		ListMenu = {
			'新建': 'categories.html?action=add',
			'编辑': function(){
				location.search = '?action=update&category_id='+$('.list-group-item.active').data('category_id');
			},
			'删除': function(){
				if($('.list-group-item.active').length < 1){
					alert('请选择需要删除的目录。');
				}else if(confirm('即将永久删除所选目录，确认？')){
					var id = $('.list-group-item.active').data('category_id');
					$.ajax({
						url: SITE_URL+'mod.php?category::delete|category_id:'+id,
						success: function(result){
							alert(result.data);
							if(result.success){
								location.reload();
							}
						},
						error: function(xhr){
							alert('与服务器的连接出现错误，分类目录删除失败。');
							console.log(xhr.responseText);
						}
					});
				}
			}
		},
		showListMenu = function(){
			if(!$('.list-group-item.active').length){
				$.showMenu({'新建': ListMenu['新建']});
			}else{
				$.showMenu(ListMenu);
			}
		};

	/** 根据请求参数显示菜单 */
	if($_GET['action']){
		$.showMenu(EditorMenu);
	}else{
		showListMenu();
	}

	$('li.list-children').prev('li').children('i').click(function(){
		var $this = $(this);
		if($this.is('.glyphicon-chevron-down')){
			$this.removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-right');
			$this.parent('li').next('li').slideUp('fast');
		}else{
			$this.removeClass('glyphicon-chevron-right').addClass('glyphicon-chevron-down');
			$this.parent('li').next('li').slideDown('fast');
		}
	});

	$('.list-group').on('click', 'li', function(event){
		event.stopPropagation();
		var $this = $(this),
			data = $this.data();
		if(!$this.is('.list-children') && event.target.nodeName != 'I'){
			if(catDetails.css('display') == 'none'){
				catList.hide();
				catDetails.slideDown('fast');
			}
			if(!$_GET['action']){
				showListMenu();
				$('.list-group-item').removeClass('active');
				$this.addClass('active');
				$('#category_name').text(data.category_name);
				$('#category_alias').text(data.category_alias);
				$('#category_desc').text(data.category_desc);
				$('#content-posts').html('加载中...');
				$.ajax({
					url: SITE_URL+'mod.php?post::getMulti|limit:3|category_id:'+data.category_id,
					success: function(result){
						if(result.success){
							var text = '包含';
							for(var i in result.data){
								text += '《'+result.data[i].post_title+'》';
							}
							text += '等 '+result.total+' 篇文章。<a href="posts.html?category_id='+data.category_id+'">查看更多 »</a>';
							$('#content-posts').html(text);
						}else{
							$('#content-posts').text(result.data);
						}
					},
					error: function(xhr){
						alert('与服务器的连接出现错误，文章信息获取失败。');
						console.log(xhr.responseText);
					}
				});
			}else{
				location.search = '?action=update&category_id='+data.category_id;
			}
		}
	})
	catEditForm.ajaxSubmit({
		success: function(result){
			if(result.success){
				alert($_GET['action'] == 'update' ? '修改成功。': '添加成功。');
				location.search = '?category_id='+result.data.category_id;
			}else{
				alert(result.data);
			}
		},
		error: function(xhr){
			alert('与服务器的连接出现错误，'+($_GET['action'] == 'update' ? '修改': '添加')+'失败。')
			console.log(xhr.responseText);
		}
	});

	/** 关闭详情页面 */
	$('#category-details>.page-header h4>i').click(function(){
		if(!$_GET['action']){
			catDetails.hide();
			catList.slideDown('fast');
			$.showMenu({'新建': ListMenu['新建']});
		}else{
			history.back(-1);
		}
	})
	if(!$_GET['category_id'] && !$_GET['action']){
		var firstLi = $('.list-group').find('li[data-category_id]').eq(0);
		if(catDetails.css('display') != 'none'){
			firstLi.trigger('click');
		}else{
			firstLi.addClass('active');
		}
	}else if($_GET['action']){
		if(catDetails.css('display') == 'none'){
			catList.hide();
			catDetails.slideDown('fast');
		}
		catEditForm.find('#category_name').focus();
	}else{
		catEditForm.find('#category_name').focus();
	}
});